package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.mapper.AccountMapper;
import org.csu.webjpetstoreback.model.Account;
import org.csu.webjpetstoreback.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public CommonResponse<Account> GetAccountByID(String userid) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Account account = accountMapper.selectOne(queryWrapper);
        if (account != null){
            return CommonResponse.BackForSuccess(account);
        }else {
            return CommonResponse.BackForError("该用户不存在");
        }
    }

    @Override
    public CommonResponse<List<Account>> GetAccount() {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        List<Account> account = accountMapper.selectList(queryWrapper);
        if (account.size() > 0){
            return CommonResponse.BackForSuccess(account);
        }else{
            return CommonResponse.BackForError("当前不存在用户");
        }
          }

    @Override
    public CommonResponse<Account> PostAccount(Account account) {
        int result = accountMapper.insert(account);
        System.out.println(result);
        if (result ==1){
            return CommonResponse.BackForSuccess(account);
        }else{
            return CommonResponse.BackForError("新增失败");
        }
        //先假设不可以为空，结果1成功
    }

    @Override
    public CommonResponse<Account> PutAccount(Account account) {
        QueryWrapper<Account> QueryWrapper = new QueryWrapper<>();
        UpdateWrapper<Account> UpdateWrapper = new UpdateWrapper<>();
//        UpdateWrapper.
//        accountMapper.
        int result = accountMapper.update(account,QueryWrapper);
        System.out.println(result+"update");
        if (result ==1){
            System.out.println("success in update");
            return CommonResponse.BackForSuccess(account);
        }else{
            return CommonResponse.BackForError("修改失败");
        }
        //先假设不可以为空，结果1成功

    }

    @Override
    public CommonResponse<Account> DeleteAccount(String userid) {
        QueryWrapper<Account> QueryWrapper = new QueryWrapper<>();
        int result = accountMapper.deleteById(userid);
        if (result ==1){
            return CommonResponse.BackForSuccess("删除成功!!!");
        }else{
            return CommonResponse.BackForError("删除失败");
        }
        //先假设不可以为空，结果1成功

    }
}
